package cn.com.cfae.iras.doc.analyze.parse.extractor;

import cn.com.cfae.iras.doc.analyze.keys.ExtractMethod;
import cn.com.cfae.iras.doc.analyze.parse.QYZXUtils;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVType;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.*;
import cn.com.cfae.iras.doc.analyze.parse.tokenizer.FXTKTokenizer;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * M3发行条款
 */
public class M3Extractor extends MTableExtractor {
    private static final Logger logger = LoggerFactory.getLogger(M3Extractor.class);

    private FXTKTokenizer fxtkTokenizer = FXTKTokenizer.getInstance();

    @Override
    public void extract() {
        try {
            extractM3();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            parseChapterSection();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            extractJzbjjdap();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            extractJkjsap();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            extractSsltap();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public M3Extractor() {
        super();
    }

    /**
     * 集中簿记建档安排
     */
    private void extractJzbjjdap() {
        long t1 = System.currentTimeMillis();
        ExtractBizModel extractBizModel = getChapterModel().getModel("JZBJJD");
        logger.info("抽取{}章节业务模型数据，抽取集中簿记建档安排......", getChapterModel().getChapterId());
        ExtractItem extractItem = extractBizModel.getExtractItem("SGYYRQ");
        String regex = "([【]{0,}\\d{4}[】]{0,1}\\s{0,}年\\s{0,}[【\\[]{0,1}\\d{1,2}[】\\]]{0,1}\\s{0,}月[\\[【]{0,1}\\s{0,}\\d{1,2}[\\]】]{0,1}\\s{0,}日)";
        Pattern pattern = Pattern.compile(regex);
        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        try {
            boolean isCandicate = false;
            for (int i = 0; i < sectionModelList.size(); i++) {
                SectionModel sectionModel = sectionModelList.get(i);
                List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
                if (CollectionUtils.isEmpty(childSectionModelList)) {
                    continue;
                }
                for (int j = 0; j < childSectionModelList.size(); j++) {
                    SectionModel childSectionModel = childSectionModelList.get(j);
                    if (childSectionModel == null) {
                        continue;
                    }
                    String title = childSectionModel.getTitle();
                    if (isEmpty(title)) {
                        continue;
                    }
                    if (isFXTK_JZBJJDAP(title)) {
                        String content = childSectionModel.getContent();
                        if (isEmpty(content)) {
                            continue;
                        }
                        Matcher matcher = pattern.matcher(content);
                        logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                        while (matcher.find()) {
                            KVText kText = new KVText();
                            TextLayout kTextLayout = cloneTextLayout(childSectionModel.getTextLayout());
                            TextLayout vTextLayout = cloneTextLayout(childSectionModel.getTextLayout());
                            kTextLayout.setText(childSectionModel.getTitle());
                            KVText vText = new KVText();
                            vText.setText(matcher.group());
                            vTextLayout.setText(vText.getText());
                            kText.add(kTextLayout);
                            vText.add(vTextLayout);
                            extractItem.setKey(kText);
                            extractItem.setValue(vText);
                            extractItem.getKey().setPageNumber(kTextLayout.getPageNumber());
                            extractItem.getValue().setPageNumber(vTextLayout.getPageNumber());
                            isCandicate = true;
                            break;
                        }
                    }
                    if (isCandicate) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章节业务模型数据，抽取集中簿记建档安排业务模型完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    /**
     * 缴款和结算安排
     */
    private void extractJkjsap() {
        long t1 = System.currentTimeMillis();
        ExtractBizModel extractBizModel = getChapterModel().getModel("JKJS");
        logger.info("抽取{}章节业务模型数据，抽取缴款结算安排......", getChapterModel().getChapterId());
        ExtractItem extractItem = extractBizModel.getExtractItem("JKR");
        String regex = "((缴款时间|缴款日):\\s?[【]{0,}\\d{4}[】]{0,1}\\s{0,}年\\s{0,}[【\\[]{0,1}\\d{1,2}[】\\]]{0,1}\\s{0,}月[\\[【]{0,1}\\s{0,}\\d{1,2}[\\]】]{0,1}\\s{0,}日)";
        Pattern pattern = Pattern.compile(regex);
        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        try {
            boolean isCandicate = false;
            for (int i = 0; i < sectionModelList.size(); i++) {
                SectionModel sectionModel = sectionModelList.get(i);
                List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
                if (CollectionUtils.isEmpty(childSectionModelList)) {
                    continue;
                }
                for (int j = 0; j < childSectionModelList.size(); j++) {
                    SectionModel childSectionModel = childSectionModelList.get(j);
                    if (childSectionModel == null) {
                        continue;
                    }
                    String title = childSectionModel.getTitle();
                    if (isEmpty(title)) {
                        continue;
                    }
                    if (isFXTK_JKJSAP(title)) {
                        String content = childSectionModel.getContent();
                        if (isEmpty(content)) {
                            continue;
                        }
                        Matcher matcher = pattern.matcher(content);
                        logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                        while (matcher.find()) {
                            KVText kText = new KVText();
                            TextLayout kTextLayout = cloneTextLayout(childSectionModel.getTextLayout());
                            TextLayout vTextLayout = cloneTextLayout(childSectionModel.getTextLayout());
                            kTextLayout.setText(childSectionModel.getTitle());
                            KVText vText = new KVText();
                            vText.setText(matcher.group());
                            vTextLayout.setText(vText.getText());
                            kText.add(kTextLayout);
                            vText.add(vTextLayout);
                            extractItem.setKey(kText);
                            extractItem.setValue(vText);
                            extractItem.getKey().setPageNumber(kTextLayout.getPageNumber());
                            extractItem.getValue().setPageNumber(vTextLayout.getPageNumber());
                            isCandicate = true;
                            break;
                        }
                    }
                    if (isCandicate) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章节业务模型数据，抽取缴款结算安排业务模型完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));

    }

    /**
     * 上市流通安排
     */
    private void extractSsltap() {
        long t1 = System.currentTimeMillis();
        ExtractBizModel extractBizModel = getChapterModel().getModel("SSLT");
        logger.info("抽取{}章节业务模型数据，抽取上市流通安排......", getChapterModel().getChapterId());
        ExtractItem extractItem = extractBizModel.getExtractItem("SSLTR");
        String regex = "([【]{0,}\\d{4}[】]{0,1}\\s{0,}年\\s{0,}[【]{0,1}\\d{1,2}[】]{0,1}\\s{0,}月[【]{0,1}\\s{0,}\\d{1,2}[】]{0,1}\\s{0,}日)";
        Pattern pattern = Pattern.compile(regex);
        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        try {
            boolean isCandicate = false;
            for (int i = 0; i < sectionModelList.size(); i++) {
                SectionModel sectionModel = sectionModelList.get(i);
                List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
                if (CollectionUtils.isEmpty(childSectionModelList)) {
                    continue;
                }
                for (int j = 0; j < childSectionModelList.size(); j++) {
                    SectionModel childSectionModel = childSectionModelList.get(j);
                    if (childSectionModel == null) {
                        continue;
                    }
                    String title = childSectionModel.getTitle();
                    if (isEmpty(title)) {
                        continue;
                    }
                    if (isFXTK_SSLTAP(title)) {
                        String content = childSectionModel.getContent();
                        if (isEmpty(content)) {
                            continue;
                        }
                        Matcher matcher = pattern.matcher(content);
                        logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                        while (matcher.find()) {
                            KVText kText = new KVText();
                            TextLayout kTextLayout = cloneTextLayout(childSectionModel.getTextLayout());
                            TextLayout vTextLayout = cloneTextLayout(childSectionModel.getTextLayout());
                            kTextLayout.setText(childSectionModel.getTitle());
                            KVText vText = new KVText();
                            vText.setText(matcher.group());
                            vTextLayout.setText(vText.getText());
                            kText.add(kTextLayout);
                            vText.add(vTextLayout);
                            extractItem.setKey(kText);
                            extractItem.setValue(vText);
                            extractItem.getKey().setPageNumber(kTextLayout.getPageNumber());
                            extractItem.getValue().setPageNumber(vTextLayout.getPageNumber());
                            isCandicate = true;
                            break;
                        }
                    }
                    if (isCandicate) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章节业务模型数据，抽取上市流通安排业务模型完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    /**
     * 抽取M3
     *
     * @param
     */
    private void extractM3() {
        try {
            extractM3Fxtk();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 发行条款
     *
     * @param
     */
    private void extractM3Fxtk() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章节发行条款业务模型数据......", getChapterModel().getChapterId());
        List<PageModel> pageModelList = getPageModelList();
        ExtractBizModel extractBizModel = getChapterModel().getModel(FXTKModel.MODEL_NAME);
        ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
        fxtkTokenizer.loadCoreSynonyms(extractBizModel);
        PageModel pageModel = null;
        ExtractMethod actualExtractMethod = null;
        boolean isFxtk_End = false;
        for (int i = 0; i < pageModelList.size(); i++) {
            pageModel = pageModelList.get(i);
            if (isFxtk_End) {
                break;
            }
            logger.info("抽取{}章节发行条款业务模型数据，当前页码：{}，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getPageNumber(), pageModel.getJsonTextData());
            if (pageModel != null) {
                List<String> extractTableDataList = null;
                ExtractMethod extractMethod = extractMethods[0];
                ExtractItem targetExtractItem = null;
                switch (extractMethod) {
                    case TEXT:
                        break;
                    case KV:
                        try {
                            extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                        }
                        if (CollectionUtils.isEmpty(extractTableDataList)) {
                            if (i > 0 && actualExtractMethod == ExtractMethod.TABLE) {
                                logger.info("抽取{}章节发行条款业务模型数据，数据格式为表格形式，表格可能抽取完毕，中断后续页码数据抽取！", getChapterModel().getChapterId());
                                break;
                            }
                            logger.info("抽取{}章节发行条款业务模型数据，数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                            JsonArray dataArray = getPageDataArray(pageModel);
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                                String combineLine = combineTextJsonArray(lineArray);
                                if (isEmpty(combineLine)) {
                                    continue;
                                }
                                if (isFXTK_END(combineLine)) {
                                    isFxtk_End = true;
                                    break;
                                }
                                logger.info("抽取{}章节发行条款业务模型数据，分析文本：{}。", getChapterModel().getChapterId(), combineLine);
                                double left = findValidTableCell(lineArray).getAsJsonObject().get("left").getAsDouble();
                                if (pageModel.getWidth() / 5 > left && left > 0) {
//                                    if (isFXTK_Format(combineLine)) {
//                                        if (findFXTK_Format_Split_Char(combineLine).length() == 0) {
//                                            continue;
//                                        }
//                                    }
                                    String key = combineLine.split(findFXTK_Format_Split_Char(combineLine), 2)[0];
                                    try {
                                        targetExtractItem = fxtkTokenizer.splitWords(extractBizModel, key);
                                        if (targetExtractItem != null) {
                                            if (isFXTK_Format(combineLine)) {
                                                setFXTK_KV(lineArray, targetExtractItem);
                                                setExtractItemPageNumber(pageModel, targetExtractItem);
                                                logger.info("抽取{}章节发行条款业务模型数据，分析到发行条款相关信息，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), combineLine);
                                            }
                                        }
                                    } catch (Exception e) {
                                        logger.error(e.getMessage(), e);
                                    }
                                }
                            }
                        } else {
                            logger.info("抽取{}章节发行条款业务模型数据,数据格式为表格形式，表格内容：{}。", getChapterModel().getChapterId(), extractTableDataList);
                            if (i == 0 && extractTableDataList.size() == 1) {
                                logger.info("抽取{}章节发行条款业务模型数据,该页码含误导信息，实际数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                                JsonElement tableData = str2Json(extractTableDataList.get(0));
                                JsonObject jsonObject = tableData.getAsJsonArray().get(0).getAsJsonObject();
                                JsonArray dataArray = jsonObject.get("data").getAsJsonArray();
                                if (dataArray.size() <= 3) {
                                    logger.info("抽取{}章节发行条款业务模型数据,执行到该处应该为发行条款首页，该页码含误导信息，实际数据格式有可能为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                                    dataArray = getPageDataArray(pageModel);
                                    for (int j = 0; j < dataArray.size(); j++) {
                                        JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                                        String combineLine = combineTextJsonArray(lineArray);
                                        if (isEmpty(combineLine)) {
                                            continue;
                                        }
                                        if (isFXTK_END(combineLine)) {
                                            isFxtk_End = true;
                                            break;
                                        }
                                        logger.info("抽取{}章节发行条款业务模型数据，分析文本：{}。", getChapterModel().getChapterId(), combineLine);
                                        double left = findValidTableCell(lineArray).getAsJsonObject().get("left").getAsDouble();
                                        if (pageModel.getWidth() / 5 > left && left > 0) {
//                                            if (isFXTK_Format(combineLine)) {
//                                                if (findFXTK_Format_Split_Char(combineLine).length() == 0) {
//                                                    continue;
//                                                }
//                                            }
                                            String key = combineLine.split(findFXTK_Format_Split_Char(combineLine))[0];
                                            try {
                                                targetExtractItem = fxtkTokenizer.splitWords(extractBizModel, key);
                                                if (targetExtractItem != null) {
                                                    if (isFXTK_Format(combineLine)) {
                                                        setFXTK_KV(lineArray, targetExtractItem);
                                                        setExtractItemPageNumber(pageModel, targetExtractItem);
                                                        logger.info("抽取{}章节发行条款业务模型数据，分析到发行条款相关信息，EXTRACTITEM：{}，KEY：{}，VALUE：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), targetExtractItem.getKey().getText(), targetExtractItem.getValue().getText());
                                                    }
                                                }
                                            } catch (Exception e) {
                                                logger.error(e.getMessage(), e);
                                            }
                                        }
                                    }
                                } else {
                                    for (int k = 0; k < dataArray.size(); k++) {
                                        int offset = 0;
                                        JsonArray itemArray = dataArray.get(k).getAsJsonArray();
                                        if (itemArray.size() <= 1) {
                                            continue;
                                        }
                                        List<JsonObject> validJsonObjectList = findValidTableCells(itemArray);
                                        if (validJsonObjectList.isEmpty()) {
                                            continue;
                                        }
                                        JsonObject keyJsonObject = validJsonObjectList.get(0);
                                        String key = keyJsonObject.get("text").getAsString();
                                        if (NumberUtils.isDigits(key)) {
                                            logger.info("抽取{}章节发行条款业务模型数据，该列可能为序号，获取下一列，后续改为动态判断。", getChapterModel().getChapterId(), key);
                                            offset = 1;
                                            key = validJsonObjectList.get(offset).getAsJsonObject().get("text").getAsString().trim();
                                        }
                                        logger.info("抽取{}章节发行条款业务模型数据,分析表格数据内容，第{}条，分析文本：{}。", getChapterModel().getChapterId(), (k + 1), itemArray.toString());
                                        try {
                                            targetExtractItem = fxtkTokenizer.splitWords(extractBizModel, replaceCRLF(key));
                                            if (targetExtractItem != null) {
                                                setFXTK_Table_KV(validJsonObjectList, offset, targetExtractItem, pageModel);
                                                setExtractItemPageNumber(pageModel, targetExtractItem);
                                                extractValue(pageModel, extractBizModel, targetExtractItem, ExtractMethod.TABLE, dataArray, k);
                                                logger.info("抽取{}章节发行条款业务模型数据,分析到发行条款业务指标，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), itemArray.toString());
                                                continue;
                                            }
                                        } catch (Exception e) {
                                            logger.error(e.getMessage(), e);
                                        }
                                    }
                                }
                            } else {
                                actualExtractMethod = ExtractMethod.TABLE;
                                for (int j = 0; j < extractTableDataList.size(); j++) {
                                    JsonElement tableData = str2Json(extractTableDataList.get(j));
                                    JsonObject jsonObject = tableData.getAsJsonArray().get(0).getAsJsonObject();
                                    JsonArray dataArray = jsonObject.get("data").getAsJsonArray();
                                    for (int k = 0; k < dataArray.size(); k++) {
                                        int offset = 0;
                                        JsonArray itemArray = dataArray.get(k).getAsJsonArray();
                                        if (itemArray.size() <= 1) {
                                            continue;
                                        }
                                        List<JsonObject> validJsonObjectList = findValidTableCells(itemArray);
                                        if (validJsonObjectList.isEmpty()) {
                                            continue;
                                        }
                                        JsonObject keyJsonObject = validJsonObjectList.get(0);
                                        String key = keyJsonObject.get("text").getAsString().trim();
                                        if (NumberUtils.isDigits(key)) {
                                            logger.info("抽取{}章节发行条款业务模型数据，该列可能为序号，获取下一列，后续改为动态判断。", getChapterModel().getChapterId(), key);
                                            offset = 1;
                                            key = validJsonObjectList.get(offset).getAsJsonObject().get("text").getAsString().trim();
                                        }
                                        logger.info("抽取{}章节发行条款业务模型数据,分析表格数据内容，第{}条，分析文本：{}。", getChapterModel().getChapterId(), (j + 1), itemArray.toString());
                                        try {
                                            targetExtractItem = fxtkTokenizer.splitWords(extractBizModel, replaceCRLF(key));
                                            if (targetExtractItem != null) {
                                                setFXTK_Table_KV(validJsonObjectList, offset, targetExtractItem, pageModel);
                                                setExtractItemPageNumber(pageModel, targetExtractItem);
                                                extractValue(pageModel, extractBizModel, targetExtractItem, ExtractMethod.TABLE, dataArray, k);
                                                logger.info("抽取{}章节发行条款业务模型数据,分析到释义业务指标，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), itemArray.toString());
                                                continue;
                                            }
                                        } catch (Exception e) {
                                            logger.error(e.getMessage(), e);
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case TABLE:
                        break;
                    case DEFAULT:
                        break;
                }
            } else {
                logger.error("抽取{}章节发行条款业务模型数据，当前页码：{}，PDF文档：{}，不存在，请检查！", getChapterModel().getChapterId(), pageModel.getPageNumber(), getDocumentModel().getPdfFilePath());
            }
        }
        fxtkTokenizer.unloadCoreSynonyms(extractBizModel);
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章节发行条款业务模型数据，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    /**
     * 1、发行人、债务融资工具名称、债务融资工具余额、发行期限、注册总金额、本期发行金额、发行日、起息日、缴款日、发行对象、兑付日、上市流通日、债权债务登记日、信用增进（担保）、信用评级、信用评级机构、集中簿记建档系统技术支持机构、票据面值、发行价格、发行利率、票据利率、票据期限
     * @param pageModel
     * @param extractBizModel
     * @param extractItem
     * @param extractMethod
     * @param dataArray
     * @param offset
     */
    private void extractValue(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        String itemName = extractItem.getItemName();
        if (isEmpty(itemName)) {
            return;
        }
        switch (itemName) {
            case "ZWRZGJYE"://债务融资工具余额
                switch (extractMethod) {
                    case KV:

                        break;
                    case TABLE:
                        extractM3Fxtk_zwrzgjye(pageModel, extractBizModel, extractItem, extractMethod, dataArray, offset);
                        break;
                }
                break;
            case "QXRQ"://起息日期
            case "JKR"://缴款日
                break;
            case "FXDX"://发行对象
                break;
            case "FXFW"://发行范围
                break;
            case "JZBJJDJSZCJG"://集中簿记建档技术支持机构
                break;
            case "FenXQX"://分销期限
                break;
            case "FXR"://发行人
                break;
            case "ZCTZSWH"://注册通知书文号
                break;
            case "XYPJJG"://信用评级机构
                extractM3Fxtk_xypjjg(pageModel, extractBizModel, extractItem, extractMethod, dataArray, offset);
                break;
            case "PJJG"://评级结果;
                extractM3Fxtk_xypj(pageModel, extractBizModel, extractItem, extractMethod, dataArray, offset);
                break;
            case "ZJ"://增进
            case "DB"://增进
                break;
            case "ZWRZGJMC"://债务融资工具名称
                extractM3Fxtk_zwrzgjmc(pageModel, extractBizModel, extractItem, extractMethod, dataArray, offset);
                break;
            case "PJMZ"://票据面值
                break;
            case "ZCZJE"://注册总金额
                extractM3Fxtk_zcje(pageModel, extractBizModel, extractItem, extractMethod, dataArray, offset);
                break;
            case "BQFXJE"://本期发行金额
                extractM3Fxtk_bqfxje(pageModel, extractBizModel, extractItem, extractMethod, dataArray, offset);
                break;
            case "FXJG"://发行价格
                break;
            case "FXLL"://发行利率
                break;
            case "CXFS"://承销方式
                break;
            case "FXFS"://发行方式
                break;
            case "ZCXS"://主承销商
                break;
            case "BJGLR"://簿记管理人
                break;
            case "DJTGJG"://登记托管机构
                break;
            case "JZBJJD"://集中簿记建档
                break;
            case "FXRQ"://发行日期
                break;
            case "DJR"://登记日
                break;
            case "DFR"://兑付日
                break;
            case "SSLTR"://上市流通日
                break;
            case "DFFS"://兑付方式
                break;
            case "DFJG"://兑付价格
                break;
            case "PJLL"://票据利率
                break;
            case "FXTKFXR"://付息日
                break;
            case "PJQX"://票据期限
                break;
            case "ZQZWDJR"://债权债务登记日
                break;
            case "SYFL"://适用法律
                break;
            case "SWTS"://税务提示
                break;
        }

    }

    /**
     * 代偿还债务融资工具余额
     */
    private void extractM3Fxtk_zwrzgjye(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        String regex = "((" + joinSynonyms(extractItem.getSynonyms()) + ")|((([0-9]{0,3}\\,{1,}){0,}([0-9]{1,3}){1,}\\.{0,}[0-9]{0,2})\\s{0,}(亿元|万元){1}))";
        JsonArray itemArray = dataArray.get(offset).getAsJsonArray();
        String text = replaceCRLF(itemArray.get(itemArray.size() - 1).getAsJsonObject().get("text").getAsString());
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        List<WordItem> wordItemList = new ArrayList<>();
        WordItem targetWordItem = null;
        int thresholdValue = 20;
        WordItem wordItem;
        while (matcher.find()) {
            wordItem = new WordItem();
            wordItem.setValue(matcher.group());
            wordItem.setOffset(matcher.start());
            wordItemList.add(wordItem);
        }
        Collections.sort(wordItemList);
        logger.info("分析抽取发行条款-债务融资工具余额业务模型数据，输出提取的业务指标内容，指标名称：{}。", extractItem.getItemName());
        wordItemList.forEach(e -> {
            logger.info("分析抽取发行条款-债务融资工具余额业务模型数据，分析定位到业务模型数据文本信息，分析文本：{}，偏移位置：{}。", e.getValue(), e.getOffset());
        });
        for (int i = 0; i < wordItemList.size(); i++) {
            WordItem wordItem1 = wordItemList.get(i);
            if (ArrayUtils.contains(extractItem.getSynonyms(), wordItem1.getValue())) {
                targetWordItem = wordItem1;
                continue;
            }
            if (isSXZC_JE(wordItem1.getValue())) {
                if (targetWordItem == null) {
                    continue;
                }
                if (targetWordItem.getOffset() > wordItem1.getOffset()) {
                    continue;
                }
                if (wordItem1.getOffset() - targetWordItem.getOffset() > thresholdValue) {
                    continue;
                }
                KVText vText = new KVText();
                vText.setText(wordItem1.getValue());
                TextLayout vTextLayout = new TextLayout();
                vTextLayout.setPageNumber(pageModel.getPageNumber());
                vTextLayout.setText(vText.getText());
                vText.add(vTextLayout);
                extractItem.addValue(vText);
            }
        }
    }

    /**
     * 信用评级结果
     *
     * @param pageModel
     * @param extractBizModel
     * @param extractItem
     * @param extractMethod
     * @param dataArray
     * @param offset
     */
    private void extractM3Fxtk_xypj(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        JsonArray itemArray = dataArray.get(offset).getAsJsonArray();
        List<JsonObject> validJsonObjectList = findValidTableCells(itemArray);
        if (validJsonObjectList.isEmpty()) {
            return;
        }
        JsonObject keyJsonObject = validJsonObjectList.get(0);
        JsonObject valueJsonObject = validJsonObjectList.get(validJsonObjectList.size() - 1);
        if (validJsonObjectList == null) {
            return;
        }
        String text = replaceCRLF(valueJsonObject.get("text").getAsString());
        switch (extractMethod) {
            case TABLE:
                List<String> pjjgList = QYZXUtils.getDefaultXypjjg();
                for (int i = 0; i < pjjgList.size(); i++) {
                    String pjjg = pjjgList.get(i);
                    if (text.contains(pjjg)) {
                        KVText kText = new KVText();
                        KVText vText = new KVText();
                        TextLayout kTextLayout = cloneTextLayout(extractItem.getKey().getTextLayout());
                        kTextLayout.setText(replaceSpecialChar(keyJsonObject.get("text").getAsString()));
                        kText.add(kTextLayout);
                        kText.setPageNumber(kTextLayout.getPageNumber());
                        TextLayout vTextLayout = createKVTextLayout(pageModel, valueJsonObject);
                        vText.add(vTextLayout);
                        vTextLayout.setText(pjjg);
                        ExtractItem xypjjgExtractItem = extractBizModel.getExtractItem("XYPJJG");
                        xypjjgExtractItem.setKey(kText);
                        xypjjgExtractItem.setValue(vText);
                        break;
                    }
                }
                break;
            case KV:
                break;
            default:
                break;
        }
    }

    private void extractM3Fxtk_xypjjg(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        JsonArray itemArray = dataArray.get(offset).getAsJsonArray();
        List<JsonObject> validJsonObjectList = findValidTableCells(itemArray);
        if (validJsonObjectList.isEmpty()) {
            return;
        }
        JsonObject keyJsonObject = validJsonObjectList.get(0);
        JsonObject valueJsonObject = validJsonObjectList.get(validJsonObjectList.size() - 1);
        if (valueJsonObject == null) {
            return;
        }
        String text = replaceCRLF(valueJsonObject.get("text").getAsString());
        switch (extractMethod) {
            case TABLE:
                String regex = "[A-C+-]{1,}";
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(text);
                if (matcher.find()) {
                    ExtractItem pjjgExtractItem = extractBizModel.getExtractItem("PJJG");
                    KVText kText = new KVText();
                    TextLayout kTextLayout = cloneTextLayout(extractItem.getKey().getTextLayout());
                    kTextLayout.setText(replaceSpecialChar(keyJsonObject.get("text").getAsString()));
                    kText.add(kTextLayout);
                    kText.setPageNumber(kTextLayout.getPageNumber());
                    KVText vText = new KVText();
                    TextLayout vTextLayout = createKVTextLayout(pageModel, valueJsonObject);
                    vTextLayout.setText(matcher.group());
                    vText.add(vTextLayout);
                    pjjgExtractItem.setKey(kText);
                    pjjgExtractItem.setValue(vText);
                }
                break;
            case KV:
                break;
            default:
                break;
        }
    }

    /**
     * 债务融资工具名称
     * @param pageModel
     * @param extractBizModel
     * @param extractItem
     * @param extractMethod
     * @param dataArray
     * @param offset
     */
    private void extractM3Fxtk_zwrzgjmc(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        switch (extractMethod) {
            case TABLE:
                break;
            case KV:
                int upLineCount = 2;
                int downLineCount = 2;
            for(int i=0;i<upLineCount;i++ ){
                
            }
                break;
            default:
                break;
        }
    }

    /**
     * 注册金额
     */
    private void extractM3Fxtk_zcje(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        switch (extractMethod) {
            case TABLE:
                break;
            case KV:

                break;
            default:
                break;
        }
    }

    /**
     * 本期发行金额
     */
    private void extractM3Fxtk_bqfxje(PageModel pageModel, ExtractBizModel extractBizModel, ExtractItem extractItem, ExtractMethod extractMethod, JsonArray dataArray, int offset) {
        switch (extractMethod) {
            case TABLE:

                break;
            case KV:

                break;
            default:
                break;
        }
    }
}